package com.ma.wechat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.model.wechat.Menu;
import com.ma.vo.wechat.MenuVo;
import com.ma.wechat.mapper.MenuMapper;
import com.ma.wechat.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> res = new ArrayList<>();
        //查询所有菜单list集合
        List<Menu> menuList = this.list();
        //遍历，找出一级菜单  parent_id = 0，返回一级菜单集合
        List<Menu> oneMenuList = new ArrayList<>();
        for(Menu menu : menuList){
            if(menu.getParentId() == 0){
                oneMenuList.add(menu);
            }
        }
        //遍历一级菜单
        for(Menu oneMenu : oneMenuList){

            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(oneMenu,menuVo);
            List<MenuVo> twoMenuList = new ArrayList<>();
            //获取每个一级菜单里面所有二级菜单  id = parent_id
            for(Menu menu : menuList){
                if(menu.getParentId().longValue() == oneMenu.getId().longValue()){
                    MenuVo menuVo1 = new MenuVo();
                    BeanUtils.copyProperties(menu,menuVo1);
                    twoMenuList.add(menuVo1);
                }
            }
            //获取到一级菜单的二级菜单，封装到一级菜单的children里
            menuVo.setChildren(twoMenuList);
            res.add(menuVo);

        }

        return res;

    }
}
